import { toast, base, getMultiLang } from 'nc-lightapp-front'
import { CREATE_PAGE } from './constant'
import './index.less'
const { NCInput } = base;

export default function (props) {
    let promise1 = new Promise((resolve, reject) => {
      getMultiLang({
        moduleId: 2011,
        domainName: 'erm',
        currentLocale: 'simpchn',
        callback: (json) => {
          resolve(json)
        }
      })
    })

    let promise2 = new Promise((resolve, reject) => {
      props.createUIDom(
        {
          pagecode: CREATE_PAGE.pagecode, // 页面编码
          appcode: CREATE_PAGE.appcode // 小应用编码
        },
        (data) => {
        resolve(data);
      })
    })

  Promise.all([promise1, promise2]).then(response => {
    let json = response[0]
    let data = response[1]
    const meta = data.template
      props.meta.setMeta(meta)

      //处理模板配置按钮
      if(data.button) {
        for(let item of data.button) {
          const key = item.key
          if(this.buttonData[key]) {
            this.buttonData[key] = Object.assign(this.buttonData[key], item)
          }
        }
      }

      //上传发票可编辑字段
      const editableKeys = [];

      // body修改内容
      meta.body.items.forEach(item => {
        if(item.visible && item.disabled === false && item.itemtype === 'number') {
          editableKeys.push(item.attrcode)
        }

        //表头 string类型字段, 自定义渲染节点
        if(item.visible && item.disabled === false && item.itemtype === 'input') {
          item.itemtype = 'customer' //修改为自定义类型
          item.render = (text, record, rowIndex) => {
            return <span>
              <NCInput 
                className="custom"
                value={record[item.attrcode].value} 
                onChange={(e) => {
                  const { transferTable } = this.props;
                  const { setRowDataByIndex } = transferTable;
                  const isValid = record['inspectstatus'].value
                  // 1: 已查验
                  if( isValid == 1 ) {
                      setRowDataByIndex.call(this, 'head', record, rowIndex)
                      toast({ content: '发票已查验,不可修改', color: 'danger' })
                      return
                  }
                  
                  record[item.attrcode].value = e;
                  if(!this.changeKeys[record.pk_erminvoice.value]) {
                    this.changeKeys[record.pk_erminvoice.value] = {head:[], body:{}} 
                  }
                  if(!this.changeKeys[record.pk_erminvoice.value].body[record.pk_erminvoice_b.value]) {
                    this.changeKeys[record.pk_erminvoice.value].body[record.pk_erminvoice_b.value] = []
                  }
                  if(this.changeKeys[record.pk_erminvoice.value].body[record.pk_erminvoice_b.value].indexOf(item.attrcode) < 0) {
                    this.changeKeys[record.pk_erminvoice.value].body[record.pk_erminvoice_b.value].push(item.attrcode)
                  }
                  console.log('changekeys:', this.changeKeys)
                  this.setState({flag:!this.state.flag})
                }}
             /></span>
          }
        }

      })

      //head修改内容, 可以修改string类型
      meta.head.items.forEach(item => {
        // 表头 number类型字段，使用transferTable自带的功能
        if(item.visible && item.disabled === false && item.itemtype === 'number') {
          editableKeys.push(item.attrcode)
        }

        //开票日期去掉hh:mm:ss
        if(item.visible && item.disabled === false && item.attrcode === 'kprq' &&item.itemtype === 'datetimepicker') {
          item.itemtype = 'datepicker'
        }
        //表头 string类型字段, 自定义渲染节点
        if(item.visible && item.disabled === false && item.itemtype === 'input') {
          item.itemtype = 'customer'
          item.render = (text, record, rowIndex) => {
            return <span>
              <NCInput 
                className="custom"
                value={record[item.attrcode].value} 
                onChange={(e) => {
                  const { transferTable } = this.props;
                  const { setRowDataByIndex } = transferTable;
                  const isValid = record['inspectstatus'].value
                  // 1: 已查验
                  if( isValid == 1 ) {
                      setRowDataByIndex.call(this, 'head', record, rowIndex)
                      toast({ content: '发票已查验,不可修改', color: 'danger' })
                      return
                  }

                  record[item.attrcode].value = e;
                  if(!this.changeKeys[record.pk_erminvoice.value]) {
                    this.changeKeys[record.pk_erminvoice.value] = {head:[], body:{}} //body[0] = ['fphm', 'fp']
                  }
                  if(this.changeKeys[record.pk_erminvoice.value].head.indexOf(item.attrcode) < 0) {
                    this.changeKeys[record.pk_erminvoice.value].head.push(item.attrcode)
                  }
                  console.log('changekeys:', this.changeKeys)

                  this.setState({flag:!this.state.flag})
                }}
             /></span>
          }
        }
      })

      this.setState({...this.state, editableItems: editableKeys})

  })
  // debugger
  // props.createUIDom(
  //   {
  //     pagecode: CREATE_PAGE.pagecode, // 页面编码
  //     appcode: CREATE_PAGE.appcode // 小应用编码
  //   },
  //   (data) => {
      
  //   }
  // )
}
